package com.itheima.area.dao;

import com.itheima.area.pojo.CustFarmland;
import com.itheima.area.pojo.CustFarmlandCrop;
import com.itheima.area.pojo.CustFarmlandDevice;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import tk.mybatis.mapper.common.Mapper;

import javax.persistence.Id;
import java.util.List;
/**
 * @PackageName com.itheima.area.dao
 * @ProjectName Ceres-Work
 * @Created by Chanly on 2020/10/14 9:21
 **/
public interface FieldDao extends Mapper<CustFarmland> {

    @Select("select * from t_cust_farmland where id=#{id} ")
    @Results(id = "findAll" , value = {
            @Result(id = true , property = "id" , column = "id"),
            @Result(property = "fid" , column = "fid"),
            @Result(property = "size" , column = "size"),
            @Result(property = "shape" , column = "shape"),
            @Result(property = "code" , column = "code"),
            @Result(property = "status",column = "status"),
            @Result(property = "custFarmlandCropList" , column = "id" ,
                    many = @Many(select = "com.itheima.area.dao.CropDao.findByFarmlandId" , fetchType = FetchType.LAZY)),
         @Result(property = "custFarmlandDeviceList",column = "id",
                    many = @Many(select = "com.itheima.area.dao.DeviceDao.findDevByFarmlandId", fetchType = FetchType.LAZY))
    })
    List<CustFarmland>findAll(Long id);

    @Delete("DELETE t_cust_farmland_crop,t_cust_farmland FROM t_cust_farmland_crop LEFT JOIN t_cust_farmland ON t_cust_farmland.`id`=t_cust_farmland_crop.`crop_land_id` WHERE t_cust_farmland.`id`=#{id}")
    void deleteCropByFarmId(Long id);

    @Delete("DELETE t_cust_farmland_device,t_cust_farmland FROM t_cust_farmland_device LEFT JOIN t_cust_farmland ON t_cust_farmland.`id`=t_cust_farmland_device.`dev_land_id` WHERE t_cust_farmland_device.dev_land_id=#{id}")
    void deleteDeviceByFarmId(Long id);

    @Insert("insert into t_cust_farmland(fid, size, shape, code, status) values (#{fid},#{size},#{shape},#{code},#{status})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")//获取自增长的Id
    void addFarmLand(CustFarmland custFarmland);

    @Insert("insert into t_cust_farmland_device(did, dev_land_id, code, operation, install, location) VALUES (#{did},#{devLandId},#{code},#{operation},#{install},#{location})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")//获取自增长的Id
    void addFarmlandDevice(CustFarmlandDevice custFarmlandDevice);

    @Insert("insert into t_cust_farmland_crop(crop_land_id, stage, attention, img, start_time, end_time,name) VALUES (#{cropLandId},#{stage}, #{attention}, #{img}, #{startTime} ,#{endTime},#{name})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")//获取自增长的Id
    void addFarmlandCrop(CustFarmlandCrop custFarmlandCrop);

    @Update("update t_cust_farmland set fid=#{fid},size=#{size}, shape=#{shape}, code=#{code}, status=#{status} where t_cust_farmland.id=#{id}")
    void updateFarmlandById(@Param("fid") Long fid,@Param("code") String code, @Param("shape") String shape,@Param("size") String size, @Param("status") String status, @Param("id") Long id);

    @Update("update t_cust_farmland_crop set stage=#{stage}, attention=#{attention}, img=#{img}, start_time=#{startTime}, end_time =#{endTime} ,name=#{name} where t_cust_farmland_crop.crop_land_id=#{id}")
    void updateCropByFarmlandId(@Param("cropLandId") String cropLandId, @Param("stage") String stage, @Param("attention") String attention, @Param("img") String img,@Param("startTime") String startTime, @Param("endTime") String endTime,@Param("name") String name ,@Param("id") Long id);

    @Update("update t_cust_farmland_device set code =#{code},did=#{did}, operation=#{operation}, install=#{install}, location=#{location} where t_cust_farmland_device.dev_land_id=#{id}")
    void updateDeviceByFarmlandId(@Param("did") Integer did, @Param("devLandId") String devLandId, @Param("code") String code, @Param("operation") String operation, @Param("install") String install, @Param("location") String location, @Param("id") Long id);

}
